#!/bin/sh

# Author    : Oleg Pahl (deface _at_ posteo.de)
# LICENSE   : MIT
# INFO      : Made for OpenBSD Russian Community
# Rev       : 1.3

clear

echo '\033[32mTHIS SCRIPT TRY TO COMPARE COMMUNTIY [ FAQ ] VER. WITH UPSTREAM\033[39m'

echo ''
echo 'GREEN - Already up to date.'
echo 'RED - Upstream released new version.'
echo ''

color() {
    COMMUNITY_DIG=$(printf "%8.3f" "$1")
    UPSTREAM_DIG=$(printf "%8.3f" "$2")
    if [ "$COMMUNITY_DIG" = "$UPSTREAM_DIG" ];
        then
            echo $3 '\t' "->" "\033[32m $COMMUNITY_DIG\033[39m" '\t' "->" " $UPSTREAM_DIG"
        else
            echo $3 '\t' "->" "\033[31m $COMMUNITY_DIG\033[39m" '\t' "->" " $UPSTREAM_DIG"
    fi
}

for f in *.html
do
    CONTENT=$(curl -is https://cvsweb.openbsd.org/cgi-bin/cvsweb/www/faq/$f?only_with_tag=HEAD)
    UPSTREAM=`echo "$CONTENT"|grep 'Revision'|sed -e 's/<[^>]*>//g'|awk '{print $2}'`
    COMMUNITY=$(git log --oneline $f | cut -d ' ' -f 2 | sed 's/[][]//g' | head -1)
    color $COMMUNITY $UPSTREAM $f
done

echo "\n= DONE ="
